#include "threadPool.h"

int threadPoolInit(threadPool_t* p_threadPool,int workerNum){
    tidArrInit(&p_threadPool->tidArr,workerNum);
    taskQueueInit(&p_threadPool->taskQueue);
    pthread_mutex_init(&p_threadPool->mutex,NULL);
    pthread_cond_init(&p_threadPool->cond,NULL);
    p_threadPool->exitflag = 0;
    return 0;
}   

//接受一个TLV结构体并处理（选择调用函数）
//要把ssize_t length改为uint32_t length
int tlvResponse(int netfd, uint8_t type,ssize_t length,char *value){
    printf("TLVRESOPESE开始处理TLV响应\n");
    TLV_t *tlv = (TLV_t *)malloc(sizeof(TLV_t));
    memset(tlv,0,sizeof(tlv));
    if (!tlv) {
        LOG(LOG_LEVEL_ERROR, "system", "Memory allocation failed for TLV_t");
        printf("Memory allocation failed for TLV_t\n");
        return -1;
    }

    tlv->type = type;
    tlv->length = (uint32_t)length;
    tlv->id = 0; // 假设序列号为0，实际应用中可能需要根据情况设置
    if(tlv->length!=0){
        memcpy(tlv->value, value, length);
        tlv->value[length]='\0';
    }else{
        tlv->value[0]='\0';
    }
    int ret = sendTlv(netfd, tlv);
    printf("TLVRESPONS_EOVER[type=0x%02X, len=%u, seq=%u, value=%s]\n",
       tlv->type, tlv->length, tlv->id,
        tlv->value);
    free(tlv);

    return ret;
}

int chooseOptin(TLV_t *tlv, int netfd, pathStack_t *pstack,MYSQL *pthreadMysql,fileMetaData_t * pthreadFileMeta) {
    printf("TLVCHOOSE_BEFIN[type=0x%02X, len=%u, seq=%u, value=%s]\n", 
       tlv->type, tlv->length, tlv->id, 
        tlv->value);
    int pthread_user_id=-1;

    if (!tlv || !pstack) {
        LOG(LOG_LEVEL_ERROR, "system", "tlv or pstack is NULL");
        return -1;
    }
    
    // 处理命令
    //用于保存返回给客户端的内容
    char buffer[1024] = {0};
    int ret = 0;
    char filename[256] = {0};
    bzero(pthreadFileMeta->path,sizeof(pthreadFileMeta->path));
    memcpy(pthreadFileMeta->path,tlv->value,strlen(tlv->value));
    switch (tlv->type) {
        case CMD_CD:
            memset(buffer, 0, sizeof(buffer)); // 清空buffer
            if(pri_cd(judgePath(pthreadFileMeta),pthreadFileMeta, pstack)!=0){

                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }else{
                return tlvResponse(netfd, CMD_RESPONSE,0,NULL);
            }
            //memcpy(buffer,tlv->value, );
        case CMD_FILE_CHECK:
            printf("request invalid\n");
            return tlvResponse(netfd, CMD_ERROR, 0, NULL);
        case CMD_LS:
            memset(buffer, 0, sizeof(buffer)); // 清空buffer
            if ( tlv->length == 0) {
                ret = pri_ls(judgePath(pthreadFileMeta), pthreadFileMeta, pstack,buffer);
            }else{
                ret = pri_ls(judgePath(pthreadFileMeta), pthreadFileMeta, pstack, buffer);
            }
            if (ret == 0) {
                return tlvResponse(netfd, CMD_RESPONSE, strlen(buffer), buffer);
            } else {
                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        case CMD_PWD: 
            memset(buffer, 0, sizeof(buffer)); // 清空buffer  
            //ret = pwd_client(buffer, pstack);
            ret = pri_pwd(buffer,pstack);
            
            if(ret == 0){  // 使用之前保存的返回值
            return tlvResponse(netfd, CMD_RESPONSE, strlen(buffer), buffer);
            } else {
            return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        case CMD_PUTS:
            printf("request invalid\n");
            return tlvResponse(netfd, CMD_ERROR, 0, NULL);
        case CMD_GETS:
            printf("request invalid\n");
            return tlvResponse(netfd, CMD_ERROR, 0, NULL);
        case CMD_MKDIR:
            ret = pri_mkdir(judgePath(pthreadFileMeta), pthreadFileMeta, pstack);
            if (ret == 0) {
                return tlvResponse(netfd, CMD_RESPONSE, 0, NULL);
            } else {
                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        case CMD_REMOVE:
            ret = pri_remove(judgePath(pthreadFileMeta),pthreadFileMeta, pstack);
            if (ret == 0) {
                return tlvResponse(netfd, CMD_RESPONSE, 0, NULL);
            } else {
                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        case CMD_RMDIR: 
            ret = pri_rmdir(judgePath(pthreadFileMeta), pthreadFileMeta, pstack);
            if (ret == 0) {
                return tlvResponse(netfd, CMD_RESPONSE, 0, NULL);
            } else {
                return tlvResponse(netfd, CMD_ERROR, 0, NULL);
            }
        default:
            printf("未找到命令\n");
            LOG(LOG_LEVEL_WARN, "system", "Unknown command type: 0x%X", tlv->type);
            return -1;
    }
}


